package com.ddshuai.easy;

import java.util.Stack;

/**
 * 翻转整数
 * @author admin
 */
public class ReverseInteger {

    public static int reverse(Integer num){

        boolean minus = false;
        if (num < 0){
            minus = true;
            num = Math.abs(num);
        }
        if (num == null){
            throw new RuntimeException("null num");
        }
        String numStr = num.toString();
        Long numValid = Long.parseLong(numStr);
        if (Math.abs(numValid) > Integer.MAX_VALUE){
            return 0;
        }
        Stack<Character> stack = new Stack<>();
        char[] chars = numStr.toCharArray();
        for (char aChar : chars) {
            stack.push(aChar);
        }
        StringBuilder reverse = new StringBuilder();
        while (!stack.empty()){
            reverse.append(stack.pop());
        }
        if (Long.parseLong(reverse.toString()) > Integer.MAX_VALUE){
            return 0;
        }
        return minus ?  Integer.parseInt("-"+reverse.toString()):Integer.parseInt(reverse.toString());
    }

    public static void main(String[] args) {
        System.out.println(Integer.MAX_VALUE);
        System.out.println(reverse(-2147483648));

    }
}
